package com.etc.controller;


import com.etc.entity.CommentResult;
import com.etc.entity.Park;
import com.etc.service.ParkService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class ParkController {
    @Autowired
    private ParkService parkService;

    @RequestMapping("park/findAllPark")
    public CommentResult findAllPark(@RequestParam(value = "start", required = false) Integer start,
                                     @RequestParam(value = "size", required = false) Integer size){
        if (start!=null&&size!=null) {
            start = start <= 0 ? 1 : start;
            size = size <= 0 ? 5 : size;

        }else{
            start=1;
            size=5;
        }
        PageHelper.startPage(start,size);
        List<Park> list = parkService.findAllPark();
        PageInfo<Park> pageInfo = new PageInfo<>(list);
        return new CommentResult(200,"success",pageInfo);
    }

    @RequestMapping("park/findByAnyCondition")
    public CommentResult findByAnyCondition(@RequestBody Map<String, Object> map,@RequestParam(value = "start", required = false) Integer start,
                                            @RequestParam(value = "size", required = false) Integer size){
        if (start!=null&&size!=null) {
            start = start <= 0 ? 1 : start;
            size = size <= 0 ? 5 : size;

        }else{
            start=1;
            size=5;
        }
        PageHelper.startPage(start,size);
        List<Park> list = parkService.findByAnyCondition(map);
        PageInfo<Park> pageInfo = new PageInfo<>(list);
        System.out.println(list);
        return new CommentResult(200,"success",pageInfo);
    }

    @RequestMapping("park/findById/{id}")
    public CommentResult findById(@PathVariable("id")Integer id)
    {
        Park result = parkService.findById(id);
        return new CommentResult(200,"success",result);
    }

}
